//
//  CircleViewController.m
//  SXHomeTownClub
//
//  Created by 周思扬 on 2018/5/28.
//  Copyright © 2018年 jsx. All rights reserved.
//

#import "CircleViewController.h"
#import "quanZiTableViewCell.h"
#import "quanZiCollectionViewCell.h"

#import "tieZiDetailsViewController.h"
#import "reMenHuaTiModel.h"
#import "quanZiListModel.h"

@interface CircleViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource>{
    
@interface CircleViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,SDCycleScrollViewDelegate>{
    NSArray *collectionDataSource;
    NSMutableArray *tableDataSource;
    NSMutableArray *tableJingXuanDataSource;
    NSArray *dataSource;
}
@property (strong ,nonatomic) UICollectionView *headerScrollCollectionView;
@end

@implementation CircleViewController

#pragma mark --初始化
- (void)addSubViews{
    self.myTableView.delegate =self;
    self.myTableView.dataSource =self;
    self.myTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.needNoTableViewDataTips =YES;
    self.baseTableview =self.myTableView;
    
    [self setUpHeaderScrollCollectionView];
    self.myTableView.tableHeaderView =self.headerScrollCollectionView;
    
    tableDataSource =[NSMutableArray array];
    tableJingXuanDataSource =[NSMutableArray array];
    
    [self.myTableView addPullToRefreshWithActionHandler:^{
        [self getInitData];
    }];
}
//数据
- (void)getInitData{
    [self initDataCollection];
    [self initDataTableViewAndGcid:@"3" AndUid:@"83"];
}

//请求列表
- (void)initDataTableViewAndGcid:(NSString *)gcid AndUid:(NSString *)uid{
    NSMutableDictionary *params =[[NSMutableDictionary alloc] init];
    [params setObject:gcid forKey:@"gcid"];
    [params setObject:uid forKey:@"uid"];
    
    [JSXHttpTool Post:[NSString stringWithFormat:@"%s/api/Quan/groupList",JSXInterface] params:params success:^(id json) {
        if ([[[json objectForKey:@"code"] stringValue] isEqualToString:@"1"]) {
            NSLog(@"%@",json);
            NSArray *list =[json objectForKey:@"list"];
            NSArray *myList =[json objectForKey:@"mylist"];
            [self->tableDataSource removeAllObjects];
            [self->tableJingXuanDataSource removeAllObjects];
            self->tableDataSource =[quanZiListModel mj_objectArrayWithKeyValuesArray:list];
            self->tableJingXuanDataSource =[quanZiListModel mj_objectArrayWithKeyValuesArray:myList];
        }
        self->dataSource =@[self->tableJingXuanDataSource,self->tableDataSource];
        [self.myTableView reloadData];
        [self.myTableView.pullToRefreshView stopAnimating];
    } failure:^(NSError *error) {
        
    }];
}

//请求网格
- (void)initDataCollection{
    [JSXHttpTool Get:[NSString stringWithFormat:@"%s/api/tag/tag_hot",JSXInterface] params:nil success:^(id json) {
        NSLog(@"圈子热门话题==%@",json);
        if ([[json objectForKey:@"code"] isEqualToString:@"1"]) {
            NSArray *data =[json objectForKey:@"data"];
            self->collectionDataSource =[reMenHuaTiModel mj_objectArrayWithKeyValuesArray:data];
        }
        [self.headerScrollCollectionView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark --UITableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [dataSource[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifter =@"quanZiTableViewCell";
    quanZiTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identifter];
    if (!cell) {
        cell =[[NSBundle mainBundle] loadNibNamed:@"quanZiTableViewCell" owner:nil options:nil].lastObject;
    }
    quanZiListModel *model =dataSource[indexPath.section][indexPath.row];
    if (indexPath.section == 0) {
        cell.cellIsJoin.hidden =YES;
    }else{
        cell.cellIsJoin.hidden =NO;
    }
    cell.cellIsJoin.tag =[model.fid integerValue];
    [cell.cellIsJoin addTarget:self action:@selector(joinQuanZi:) forControlEvents:UIControlEventTouchUpInside];
    
    SDCycleScrollView *CycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, 18) delegate:self placeholderImage:nil];
    CycleScrollView.scrollDirection = UICollectionViewScrollDirectionVertical;
    CycleScrollView.onlyDisplayText = YES;
    NSMutableArray *titlesArray = [NSMutableArray new];
    [titlesArray addObject:model.lastpost];
    [titlesArray addObject:model.lastpost];
    CycleScrollView.titlesGroup = [titlesArray copy];
    CycleScrollView.titleLabelBackgroundColor =[UIColor whiteColor];
    CycleScrollView.autoScrollTimeInterval =3;
    CycleScrollView.titleLabelTextFont =[UIFont systemFontOfSize:13];
    CycleScrollView.titleLabelTextColor =[UIColor colorWithRed:0.56 green:0.56 blue:0.56 alpha:1.00];
    [cell.cellScrollView addSubview:CycleScrollView];
    CycleScrollView.sd_layout.leftSpaceToView(cell.cellScrollView, -10).rightSpaceToView(cell.cellScrollView, 0).topSpaceToView(cell.cellScrollView, 0).bottomSpaceToView(cell.cellScrollView, 0);
    cell.cellScrollView.userInteractionEnabled =NO;
    
    [cell.cellImage sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:[UIImage imageNamed:@"head01"]];
    cell.cellName.text =model.name;
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.myTableView deselectRowAtIndexPath:indexPath animated:YES];
    quanZiListModel *model =dataSource[indexPath.section][indexPath.row];
    if ([model.name isEqualToString:@"每日一贴"]) {
        [self.tabBarController setSelectedIndex:2];
    }else{
        tieZiDetailsViewController *tieZi =[[tieZiDetailsViewController alloc] init];
        tieZi.myFID =model.fid;
        [self.navigationController pushViewController:tieZi animated:YES];
    }
    
}
- (void)joinQuanZi:(UIButton *)sender{
    NSMutableDictionary *params =[[NSMutableDictionary alloc] init];
    [params setObject:@(sender.tag) forKey:@"fid"];
    [params setObject:@(83) forKey:@"uid"];
    [JSXHttpTool Post:[NSString stringWithFormat:@"%s/api/Quan/join",JSXInterface] params:params success:^(id json) {
        if ([[[json objectForKey:@"code"] stringValue] isEqualToString:@"1"]) {
            [self getInitData];
        }else{
        }
        [[TKAlertCenter defaultCenter] postAlertWithMessage:[json objectForKey:@"msg"]];
    } failure:^(NSError *error) {
        
    }];
}
//hader
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }else{
        if ([dataSource[section] count] > 0) {
            return 40;
        }else{
            return 1;
        }
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }else{
        if ([dataSource[section] count] > 0) {
            UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
            view.backgroundColor =[UIColor whiteColor];
            UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(10, 15, 120, 15)];
            label.text =@"精选推荐";
            label.textColor =[UIColor colorWithRed:0.61 green:0.61 blue:0.61 alpha:1.00];
            label.font =[UIFont systemFontOfSize:15];
            [view addSubview:label];
            return view;
        }else{
            return nil;
        }
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 10;
    }else{
        return 0.1;
    }
}
#pragma mark --店铺网格
- (void)setUpHeaderScrollCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    
    if (!self.headerScrollCollectionView) {
        self.headerScrollCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0,  kScreenWidth, 150) collectionViewLayout:flowLayout];
        //注册cell单元格
        [self.headerScrollCollectionView registerNib:[UINib nibWithNibName:@"quanZiCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"quanZiCollectionViewCell"];
        self.headerScrollCollectionView.delegate = self;
        self.headerScrollCollectionView.dataSource = self;
        self.headerScrollCollectionView.showsHorizontalScrollIndicator =NO;
        self.headerScrollCollectionView.backgroundColor =[UIColor whiteColor];
        
    }
    
}
/**
 *  UICollectionViewCell的个数
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return collectionDataSource.count;
}

/**
 *  每个UICollectionView展示的内容
 */
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * CellIdentifier = @"quanZiCollectionViewCell";
    quanZiCollectionViewCell *headerScrollCell  = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    headerScrollCell.layer.masksToBounds =YES;
    headerScrollCell.layer.cornerRadius =5;
    reMenHuaTiModel *model =collectionDataSource[indexPath.row];
    headerScrollCell.cellName.text =model.tagname;
    [headerScrollCell.cellImage sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:[UIImage imageNamed:PlaceHolderImg_Group]];
    
    return headerScrollCell;
}

/**
 *  定义每个UICollectionView 的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width =kScreenWidth/2.5;
    return CGSizeMake(width,width * 0.592);
}
/**
 *  定义每个UICollectionView 的 margin
 */
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0);
}
/**
 *  定义每个UICollectionView 的横向间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
/**
 *  定义每个UICollectionView 的 竖向间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}
#pragma mark --UICollectionViewDelegate
/**
 *  UICollectionView被选中时调用的方法
 */
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}
@end
